package com.zhanghe.study.java8.time;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author zh
 * @date 2021/6/1 18:08
 */
public class TestTimer {
    public static void main(String[] args) {
        Timer timer = new Timer();

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("测试定时任务"+System.currentTimeMillis());
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };


        TimerTask task1 = new TimerTask() {
            @Override
            public void run() {
                System.out.println("还会被执行吗");

            }
        };
        // TimerTask task, long delay
        // 延迟delay毫秒执行，只执行一次
        timer.schedule(task1,1000);
         // TimerTask task, long delay, long period
        // 延迟delay毫秒执行,每隔period毫秒执行一次
        timer.schedule(task,100,1000);
        timer.scheduleAtFixedRate(task,1000,1000);
        // 取消任务
//        timer.cancel();
//        timer.purge();


    }
}
